<template>
  <Panel>
    <template #tit>
      管理信息
    </template>
    <template>
      <ul>
        <li>
          <span>管理员:</span>
          <p>{{ users.id }}</p>
        </li>
        <li>
          <span>账号:</span>
          <p>{{ users.account }}</p>
        </li>
        <li>
          <span>用户组:</span>
          <p>{{ users.userGroup }}</p>
        </li>
        <li>
          <span>创建时间:</span>
          <p>{{ users.ctime | format }}</p>
        </li>
        <li>
          <span class="spanImg">管理员头像:</span>

          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:5000/users/avatar_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </li>
        <li>
          <el-button type="primary" @click="saveImg">确认上传</el-button>
        </li>
      </ul>
    </template>
  </Panel>
</template>

<script>
//引入本地存储文件,获取存储的数据
import local from '../../utils/local'
//11.导出修改用户头像的功能函数
import { editUserImg } from '../../api/users'
export default {
  data() {
    return {
      users: {},
      imageUrl: '',
      str: ''
    }
  },
  created() {
    this.users = JSON.parse(local.get('user'))
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      // console.log(this.imageUrl, 'hahahah')
      // console.log(res, '滴滴滴滴')
      this.str = res.imgUrl
      // console.log(this.str, '11111111111')
    },
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg'
      const arr = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!arr.includes(file.type)) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return arr.includes(file.type) && isLt2M
    },
    //发送请求修改头像
    async saveImg() {
      const { code, msg } = await editUserImg({
        imgUrl: this.str
      })
      if (code === 0) {
        this.$message.success(msg)
        location.reload()
      } else {
        this.$message.error(msg)
      }
    }
  }
}
</script>

<style lang="less" scoped>
ul {
  margin: 0;
}
li {
  display: flex;
  list-style: none;
  line-height: 26px;
  margin: 10px 0;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
  span {
    flex: 0 0 100px;
    margin-right: 15px;
  }
  p {
    margin: 0;
  }
  .spanImg {
    line-height: 100px;
  }
  .el-avatar {
    width: 100px;
    height: 100px;
  }
}
//穿刺写法
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
/deep/.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}
.avatar {
  width: 148px;
  height: 148px;
  display: block;
}
</style>
